package com.gonfre.quientelo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;


public class SortFragment extends Fragment {

    //Options to initialize
    public static final int PURCHASER = 0;
    public static final int SELLER    = 1;

    //Different options selected
    public static final int NONE        = -1;
    public static final int NEWEST      = 0;
    public static final int OLDEST      = 1;
    public static final int DESCRIPTION = 2;
    public static final int PRICE       = 3;
    public static final int USER        = 4;
    public static final int PHOTO       = 5;
    public static final int NO_PHOTO    = 6;

    //Radios Buttons
    private RadioButton priceRadio;
    private RadioButton descriptionRadio;
    private RadioButton userRadio;
    private RadioButton oldestRadio;
    private RadioButton newestRadio;
    private RadioButton photoRadio;
    private RadioButton noPhotoRadio;

    //Buttons
    private Button okButton;
    private Button cancelButton;

    //Values
    private int currentValue = NEWEST;
    private int userType = SELLER;

    /* METHODS */
    public SortFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sort, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Get radios
        priceRadio       = (RadioButton) getActivity().findViewById(R.id.rdio_pre);
        descriptionRadio = (RadioButton) getActivity().findViewById(R.id.rdio_des);
        userRadio        = (RadioButton) getActivity().findViewById(R.id.rdio_user);
        newestRadio      = (RadioButton) getActivity().findViewById(R.id.rdio_nue);
        oldestRadio      = (RadioButton) getActivity().findViewById(R.id.rdio_vie);
        photoRadio       = (RadioButton) getActivity().findViewById(R.id.rdio_cfo);
        noPhotoRadio     = (RadioButton) getActivity().findViewById(R.id.rdio_sfo);

        // Get Buttons
        okButton     = (Button) getActivity().findViewById(R.id.btnSortOk);
        cancelButton = (Button) getActivity().findViewById(R.id.btnSortCancel);

        // Add Listeners to buttons
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectRadioFromCurrentValue();
                ((MyActivity)getActivity()).onSorterBarCancelPressed();
            }
        });

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentValue = getValueFromSelectedRadio();
                ((MyActivity)getActivity()).onSorterBarOkPressed(currentValue);
            }
        });

        //Initialize
        initialize();
    }

    //Hide & show radios
    public void initialize() {
        setCurrentValue(NEWEST);

        if (userType == SELLER) {
            priceRadio.setVisibility(View.GONE);
            photoRadio.setVisibility(View.GONE);
            noPhotoRadio.setVisibility(View.GONE);

            userRadio.setText("Comprador");
        } else {
            descriptionRadio.setVisibility(View.GONE);

            userRadio.setText("Vendedor");
        }
    }

    //Set/Get values
    public int getCurrentValue() {
        return currentValue;
    }

    public void setCurrentValue(int value) {
        currentValue = value;
        selectRadioFromCurrentValue();
    }

    public void setUserType(int type) {
        userType = type;
    }

    // Values Vs Radios
    private void selectRadioFromCurrentValue() {
        switch (currentValue) {
            case PRICE:
                priceRadio.setChecked(true);
                break;

            case DESCRIPTION:
                descriptionRadio.setChecked(true);
                break;

            case USER:
                userRadio.setChecked(true);
                break;

            case OLDEST:
                oldestRadio.setChecked(true);
                break;

            case NEWEST:
                newestRadio.setChecked(true);
                break;

            case PHOTO:
                photoRadio.setChecked(true);
                break;

            case NO_PHOTO:
                noPhotoRadio.setChecked(true);
                break;
        }
    }

    private int getValueFromSelectedRadio() {
        if (descriptionRadio.isChecked()) {
            return DESCRIPTION;
        } else if (priceRadio.isChecked()) {
            return PRICE;
        } else if (userRadio.isChecked()) {
            return USER;
        } else if (oldestRadio.isChecked()) {
            return OLDEST;
        } else if (newestRadio.isChecked()) {
            return NEWEST;
        } else if (photoRadio.isChecked()) {
            return PHOTO;
        } else if (noPhotoRadio.isChecked()) {
            return NO_PHOTO;
        }

        return NONE;
    }
}
